
$("#leftselect").dblclick(function(){//绑定双击事件
    //获取全部的选项，删除并追加给对方
    $("option:selected",this).appendTo("#sel-hostinput");//追加给对方
});


$("#sel-hostinput").dblclick(function(){//绑定双击事件
    //获取全部的选项，删除并追加给对方
    $("option:selected",this).appendTo("#leftselect");//追加给对方
});

$("#addTemplate").click(function() {
    var templateName = $("#inputTemplate").val();
	var ajax = $.ajax({
		type: "POST",
		url: '/addtemplatemanager',
		timeout: 5000,
		data: {'templatename': templateName},
		success: do_server_status,
		dataType: 'json',
		async: true,
	});
});
$("#flushtodb").click(function(){
  var ajax = $.ajax({
		type: "GET",
		url: '/flushHostConfigToDb',
		timeout: 5000,
		success: do_server_status,
		dataType: 'json',
		async: true,
	});
});

$("#btn-addHost").click(function() {
    var inputHostName = $("#exampleInputHost").val();
    var selectHost  = $("#sel-hostinput").val();
    $("#sel-hostinput option").each(function(){
			inputHostName = (inputHostName +"," + $(this).text()); //这里得到的就是
	});
    var templatetype = $("#templatetype").val()
    var url ="/addhosttotemplate";
    if (templatetype ==1){
         url ="/addservicetotemplate";
    }

    var templateName =  $("#templatename").val();
	var ajax = $.ajax({
		type: "POST",
		url: url,
		timeout: 5000,
		data: {'hostName': inputHostName,'templatename':templateName},
		success: do_server_status,
		dataType: 'json',
		async: true,
	});
});


function do_server_status(data){
    if(data.success==0 ){
        location.reload();
     }
    else{
        alert("添加失败");
    }
}

$('#myModal').on('show.bs.modal', function (event) {
  var button = $(event.relatedTarget) // Button that triggered the modal
  var recipient = button.data('type') // Extract info from data-* attributes
  var templateName  = button.data('templatename')

  //从后台根据模版名称，查询主机列表。

  var selOpt = $("#sel-hostinput option");
  selOpt.remove();
  var selObj = $("#sel-hostinput");

  var modal = $(this)
  var message = ""
  var url =""
  if(recipient == 0){
     message = "添加主机";
     url = "/getHostListByTemplateName";
  }
  else{
     url = "/getServiceListByTemplateName"
     message = "添加服务";
  }
  var ajax = $.ajax({
        type: "GET",
        url: url,
        timeout: 5000,
        data: {'templatename':templateName},
        success: function(data){
          datalist = data.data.toString().split(",");
          for (var i=0;i<datalist.length;i++)
            {
               selObj.append("<option value='"+datalist[i]+"'>"+datalist[i]+"</option>");
            }
        },
        dataType: 'json',
        async: true,
  });
  modal.find('.modal-title').text(message)
  modal.find('#templatename').val(templateName)
  modal.find('#templatetype').val(recipient)

})